package com.warmlight.voicepacket.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.MypalyModalCancel;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.MBaseAdapter;
import com.warmlight.voicepacket.data.PlaydeatalData;
import com.warmlight.voicepacket.data.RecommendFeed;
import com.warmlight.voicepacket.data.TempData;
import com.warmlight.voicepacket.data.eventbus.PlayerServiceData;
import com.warmlight.voicepacket.utils.ClickLog;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.warmlight.voicepacket.utils.ToastUtil;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 我的播单详情  登录后
 */
public class MyCoverDetailNetAdpter  extends MBaseAdapter {

    private FragmentManager fragment;
    private  Boolean isBodan;
    public MyCoverDetailNetAdpter(Context context, List<PlaydeatalData.DataBean.ListBean> list , FragmentManager fragmentManager, Boolean isbodan) {
        super(context, list);
        fragment=fragmentManager;
        isBodan=isbodan;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyCoverDetailNetAdpter.ViewHolder holder;
        if (convertView != null) {
            holder = (MyCoverDetailNetAdpter.ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(mContext, R.layout.item_playdetail_online_mine, null);
            holder = new MyCoverDetailNetAdpter.ViewHolder();
            holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
//            holder.iv_gengduo = (ImageView) convertView.findViewById(R.id.iv_gengduo);
            holder.iv_righticon = (ImageView) convertView.findViewById(R.id.iv_righticon);
            holder.iv_rightshoucang = (ImageView) convertView.findViewById(R.id.iv_rightshoucang);
            holder.tv_cover_name = (TextView) convertView.findViewById(R.id.tv_cover_name);
            holder.iv_ischeck = (ImageView) convertView.findViewById(R.id.iv_ischeck);
            holder.tv_audit_failure = (TextView) convertView.findViewById(R.id.tv_audit_failure);
            convertView.setTag(holder);
        }

        final PlaydeatalData.DataBean.ListBean listBean = (PlaydeatalData.DataBean.ListBean) (mList.get(position));
        final int isSelect = listBean.getIsSelect();
        if (isSelect == 0){
            holder.iv_ischeck.setVisibility(View.GONE);
            holder.tv_num.setVisibility(View.VISIBLE);
            holder.iv_rightshoucang.setVisibility(View.VISIBLE);
        }else{
            holder.iv_ischeck.setVisibility(View.VISIBLE);
            holder.tv_num.setVisibility(View.GONE);
            holder.iv_rightshoucang.setVisibility(View.GONE);
            if (isSelect==1){
                holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_no);
            }else{
                holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_yes);
            }

        }

        if(((PlaydeatalData.DataBean.ListBean)(mList.get(position))).isCheak()){//如果是播放 。。。做个判断
            holder.tv_num.setBackgroundResource(R.drawable.icon_playing_new);
            holder.tv_num.setText("");
            holder.tv_title.setTextColor(Color.parseColor("#FC0719"));
//            holder.tv_time.setText(((PlaydeatalData.DataBean.ListBean)(mList.get(position))).getDuration());
        }else {
            if (((PlaydeatalData.DataBean.ListBean)(mList.get(position))).isPause()){
                holder.tv_num.setBackgroundResource(R.drawable.icon_pause_new);
                holder.tv_num.setText("");
                holder.tv_title.setTextColor(Color.parseColor("#CE3838"));
            }else {
                holder.tv_num.setBackgroundResource(R.color.color_fff);
                holder.tv_title.setTextColor(Color.parseColor("#333333"));
                holder.tv_num.setText(position + 1 + "");
            }
//            holder.tv_time.setText("");
        }
        holder.tv_cover_name.setText(listBean.getCover_name());
//        if (!isBodan){//这里判断是否是本地播放页面
//            holder.iv_righticon.setImageResource(R.drawable.icon_playshare_deep);
//            holder.iv_rightshoucang.setImageResource(R.drawable.icon_paly_shoucang);
//        }else {
        holder.iv_righticon.setVisibility(View.GONE);
//        }

//        String type = listBean.getType();
        final String status = listBean.getStatus();

        if (TextUtils.equals("1",status)){
            holder.tv_audit_failure.setVisibility(View.GONE);
        }else{
            holder.tv_audit_failure.setVisibility(View.VISIBLE);
        }

        holder.tv_title.setText(((PlaydeatalData.DataBean.ListBean)(mList.get(position))).getTitle());
        holder.iv_rightshoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.equals("-1",status)){
                    ToastUtil.showShort(mContext,"该条语音未审核通过，暂不支持分享");

                    TempData.AudioData = changeBean((PlaydeatalData.DataBean.ListBean) mList.get(position));
                    Intent intent = new Intent(mContext, MypalyModalCancel.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("mypaly",(PlaydeatalData.DataBean.ListBean)mList.get(position));
                    bundle.putString("status","-1");
                    intent.putExtras(bundle);
                    //本地播单单条编辑点点点埋点
//                    MobclickAgent.onEvent(mContext.getApplicationContext(), MobClickHelper.MYCLOT_FEED_MORE,((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle());
//                    HashMap<String,Object> temp = new HashMap<>();
//                    temp.put("本地播单单条编辑点点点埋点",((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
//                    ZhugeSDK.getInstance().track(mContext.getApplicationContext(),MobClickHelper.MYCLOT_FEED_MORE,temp);

                    mContext.startActivity(intent);

                    return;
                }else{
                }
                TempData.AudioData = changeBean((PlaydeatalData.DataBean.ListBean) mList.get(position));
                Intent intent = new Intent(mContext, MypalyModalCancel.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("mypaly",(PlaydeatalData.DataBean.ListBean)mList.get(position));
                intent.putExtras(bundle);
                //本地播单单条编辑点点点埋点
                MobclickAgent.onEvent(mContext.getApplicationContext(), MobClickHelper.MYCLOT_FEED_MORE,((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle());
                HashMap<String,Object> temp = new HashMap<>();
                temp.put("本地播单单条编辑点点点埋点",((PlaydeatalData.DataBean.ListBean) mList.get(position)).getTitle().toString());
                ZhugeSDK.getInstance().track(mContext.getApplicationContext(),MobClickHelper.MYCLOT_FEED_MORE,temp);

                mContext.startActivity(intent);

                //
                ClickLog.clickLog(ClickLog.myclot_feed_more,listBean.getCover_id(),listBean.getId());

            }
        });

        holder.iv_ischeck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectItem(position);
            }
        });
        return convertView;
    }

    public void startManager(){
        //暂停所有的音乐
        EventBus.getDefault().post(new PlayerServiceData(PlayerServiceData.STOP, "stop"));
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(1);
            o.setCheak(false);
            o.setPause(false);
        }
        notifyDataSetChanged();
    }

    public void selectAll(){
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(2);
        }
        notifyDataSetChanged();
    }

    public void remove(int arg0) {//删除指定位置的item
        mList.remove(arg0);
        this.notifyDataSetChanged();//不要忘记更改适配器对象的数据源
    }

    public void insert(PlaydeatalData.DataBean.ListBean item, int arg0) {//在指定位置插入item
        mList.add(arg0, item);
        this.notifyDataSetChanged();
    }

    public void cancelManager() {
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(0);
        }
        notifyDataSetChanged();
    }

    public void cancelSelectAll() {
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            o.setIsSelect(1);
        }
        notifyDataSetChanged();
    }

    public void selectItem(int position) {
        PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(position);
        int isSelect = o.getIsSelect();
        if (isSelect==1){
//            holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_yes);
            o.setIsSelect(2);
        }else{
//            holder.iv_ischeck.setImageResource(R.drawable.icon_iv_check_no);
            o.setIsSelect(1);
        }
        notifyDataSetChanged();
    }

    public ArrayList<PlaydeatalData.DataBean.ListBean> getSelect2() {
        ArrayList<PlaydeatalData.DataBean.ListBean> select = new ArrayList<>();
        for (int i=0;i<getCount();i++) {
//            Map<String,String> map = new HashMap<>();
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            int isSelect = o.getIsSelect();
            if (isSelect == 2) {
//                map.put("id",o.getId());
//                map.put("type",o.getType());
                select.add(o);
            }
        }
        return select;
    }

    public ArrayList<String> getAllId() {
        ArrayList<String> select = new ArrayList<>();
        for (int i=0;i<getCount();i++) {
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
//            int isSelect = o.getIsSelect();
//            if (isSelect == 2) {
                select.add(o.getId());
//            }
        }
        return select;
    }
    public void deleteList(ArrayList<String> select2) {
        for (int i=0;i<select2.size();i++) {
            String s = select2.get(i);
            for (int j=0;j<getCount();j++){
                PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(j);
                if (o.getId().equals(s)){
                    mList.remove(j);
                    break;
                }
            }


        }
        notifyDataSetChanged();
    }

    public void setPublic(String id, String is_open) {
        for (int i=0;i<getCount();i++){
            PlaydeatalData.DataBean.ListBean o = (PlaydeatalData.DataBean.ListBean) mList.get(i);
            String id1 = o.getId();
            if (TextUtils.equals(id,id1)) {
//                map.put("id",o.getId());
//                map.put("type",o.getType());
                o.setIs_open(is_open);
                break;
            }
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {
        TextView tv_num;
        TextView tv_title;
        TextView tv_time;
        //        ImageView iv_gengduo;
        ImageView iv_righticon;
        ImageView iv_rightshoucang;
        TextView tv_cover_name;
        ImageView iv_ischeck;
        TextView tv_audit_failure;
    }

    private RecommendFeed.DataBean.ListBean changeBean(PlaydeatalData.DataBean.ListBean temp){
        RecommendFeed.DataBean.ListBean bean = new RecommendFeed.DataBean.ListBean();
        bean.setTitle(temp.getTitle());
        bean.setId(temp.getId());
        bean.setUrl(temp.getUrl());
        bean.setDuration(temp.getDuration());
        bean.setStar(temp.getStar());
        bean.setShare(temp.getShare());
        bean.setCollect(temp.getCollect());
        bean.setCover_id(temp.getCover_id());
        return bean;
    }
}
